<?php
namespace Api\Model;

use Think\Model;

class AreaModel extends Model {

    /**
     * @name 获取省份列表
     */
    public function getProvince() {
        return $this->where(" `level` = 1 ")->field('id,areaname,shortname,pinyin,pinyin_initials,lng,lat,position')->order('sort ASC')->select();
    }

    /**
     * @name 获取城市列表
     * @param $provinceId
     */
    public function getCity() {
        return $this->where("`level`=2")->field('id,areaname,shortname,pinyin,pinyin_initials,lng,lat,position')->order('pinyin_initials ASC')->select();
    }

    /**
     * @name 根据城市id获取地区列表
     * @param $cityId
     */
    public function getAreaByCityId($cityId) {
        return $this->where(" `parentid` = {$cityId} ")->field('id,areaname,shortname,pinyin,pinyin_initials,lng,lat,position')->order('sort ASC')->select();
    }

    /**
     * @name 根据城市名称获取地区列表
     * @param $cityId
     */
    public function getAreaByCityName($cityName) {
        $cityId = $this->where(" `areaname` = {$cityName} ")->getField('id');
        return $this->getAreaByCityId($cityId);
    }

    /**
     * @name 根据城市id获取商区列表
     * @param $areaId
     */
    public function getBusinessDistrictByCityId($cityId) {
        return $this->where(" `parentid` = {$cityId} ")->field('id,areaname,shortname,pinyin,pinyin_initials,lng,lat,position')->order('sort ASC')->select();
    }

    /**
     * @name 根据城市id获取商区列表
     * @param $areaId
     */
    public function getBusinessDistrictByCityName($cityName) {
        $cityId = $this->where(" `areaname` like '%{$cityName}%' ")->getField('id');
        if ($cityId) {
            return $this->getAreaByCityId($cityId);
        } else {
            return false;
        }

    }

}